using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
//堆叠组件
public class StackComponent : BaseComponent
{
    public List<Entity> StackObjectLists = new List<Entity>(); // 记录堆叠的实体列表

    public int XCount;
    public int YCount;
    public int ZCount; //堆叠三维面积

    public Vector3 backRot; //堆叠物体的背面朝向
    public int MaxCount
    {
        get
        {
            return XCount * YCount * ZCount;
        }
    }


    public void AddStack(Entity StackObject)
    {
        if (StackObject == null)
        {
            Debug.LogError("试图堆叠一个完全不存在的物体");
            return;
        }

        if (StackObjectLists.Contains(StackObject))
        {
            Debug.LogWarning("试图重复堆叠同一个物体");
            return;
        }
        if (StackObjectLists.Count >= MaxCount)
        {
            StackObject.Recycle();
        }
        else
        {
            StackObjectLists.Add(StackObject);
            RearrangeResource();
        }
    }

    public void RemoveStack(Entity StackObject)
    {
        if (StackObject == null)
        {
            Debug.LogError("试图移除一个完全不存在的物体");
            return;
        }

        if (!StackObjectLists.Contains(StackObject))
        {
            Debug.LogWarning("试图移除一个不存在的物体");
            return;
        }

        StackObjectLists.Remove(StackObject);
        RearrangeResource();
    }

    /// <summary>
    /// 重新排列资源
    /// </summary>
    /// <param name="resourceState"></param>
    /// <param name="resourceBase"></param>
    public void RearrangeResource()
    {
        if (StackObjectLists.Count == 0)
        {
            return;
        }

        for (int i = 0; i < StackObjectLists.Count; i++)
        {
            FlyToStackComponent flyToStackComponent = StackObjectLists[i].GetEcsComponent<FlyToStackComponent>();
            Vector3 newPosition = GetStackObjectPosition(i, flyToStackComponent.StackOffsetX, flyToStackComponent.StackOffsetY, flyToStackComponent.StackOffsetZ);
            StackObjectLists[i].transform.position = newPosition;
            StackObjectLists[i].transform.SetParent(entity.transform);
            StackObjectLists[i].transform.transform.localRotation = Quaternion.Euler(backRot);
            // StackObjectLists[i].transform.localRotation = entity.transform.rotation;
        }
    }


    public Vector3 GetStackObjectPosition(int index, float stackOffsetX, float stackOffsetY, float stackOffsetZ)
    {
        Vector3 targetPosition = entity.transform.position;
        int stackIndex = index; // 获取当前堆叠的数量
                                //物体先堆叠X轴，当X轴堆叠满了，则Z轴+1，当Z轴满了，则Y+1
                                // 分别计算当前物体在X、Y、Z轴上的堆叠索引

        int StackIndexX = stackIndex % (XCount * ZCount) % XCount; // 计算堆叠索引
        int StackIndexY = stackIndex / (XCount * ZCount); // 计算Y轴堆叠索引
        int StackIndexZ = stackIndex % (XCount * ZCount) / XCount; // 计算Z轴堆叠索引

        if (StackObjectLists.Count > 0)
        {
            targetPosition = entity.transform.position +
            entity.transform.right * stackOffsetX * StackIndexX +
             entity.transform.forward * stackOffsetZ * StackIndexZ +
             entity.transform.up * stackOffsetY * StackIndexY;
        }
        return targetPosition;
    }

    public float GetMaxPercent()
    {
        return (float)StackObjectLists.Count / MaxCount;
    }
}
